home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 May: Tool Chest / Dev.CD May 97 TC.toast / Sample Code / Overview / Signals / CTestSignal.c next >
Encoding:
C/C++ Source or Header  |  1994-11-18  |  2.4 KB  |  108 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. #
  3. #    Apple Macintosh Developer Technical Support
  4. #
  5. #    Exception handling for MPW Pascal, MacApp and MPW C
  6. #
  7. #    UFailure (aka Signals) - “Exceptional code, with a few exceptions.”
  8. #
  9. #    CTestSignal.c    -    Test tool for C access to enhanced UFailure
  10. #
  11. #    Copyright © 1985-1988 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    1.00                11/88
  15. #                1.01                06/92
  16. #
  17. #    Components:    CTestSignal.c        November 1, 1988
  18. #                CTestSignal.make    November 1, 1988
  19. #                PTestSignal.p        November 1, 1988
  20. #                PTestSignal.make    November 1, 1988
  21. #                UFailure.p            November 1, 1988
  22. #                UFailure.h            November 1, 1988
  23. #                UFailure.incl.p        November 1, 1988
  24. #                UFailure.a            November 1, 1988
  25. #
  26. #    UFailure (or Signals) is a set of exception handling routines suitable for
  27. #    use with MacApp, MPW C, and MPW Pascal. It is a jazzed-up version of the MacApp
  28. #    UFailure unit. There is a set of C interfaces to it as well.
  29. #
  30. ------------------------------------------------------------------------------*/
  31.  
  32. #include    <Types.h>
  33. #include    <stdio.h>
  34. #include    <Errors.h>
  35. #include    <UFailure.h>            /* C interfaces to UFailure stuff */
  36.  
  37.  
  38. FailInfo    fi;
  39.  
  40.  
  41. pascal void Handler(short code, long message)
  42. {
  43.     fprintf(stdout, "Handler() from Never(); message= %d, code= %d\n", message, code);
  44.     /* this will do an implicit Failure() when it exits */
  45. }
  46.  
  47.  
  48. void Never()
  49. {
  50.     short        code;
  51.     
  52.     CatchCFailures(&fi, Handler);    /* any caught by this one go to Handler */
  53.  
  54.     if (code = CatchSignal()) {        /* if we’re getting a signal then… */
  55.         fprintf(stdout, "Never() shouldn’t get here; code= %d\n", code);
  56.         return;
  57.     }
  58.     
  59.     FreeSignal();                    /* “pop” the last Catchxxx */
  60.     
  61.     SignalMessage(7, 77777);
  62. }
  63.  
  64.  
  65. void Failer()
  66. {
  67.     if (!CatchSignal())
  68.         Never();                    /* call Never; if a signal comes, fall through */
  69.  
  70.     Failure(69, 0);                    /* fail no matter what */
  71. }
  72.  
  73.  
  74. long Value()
  75. {
  76.     short        code;
  77.     
  78.     if (code = CatchSignal()) {
  79.         fprintf(stdout, "Shouldn’t be here in Value(); code= %d\n", code);
  80.         return code;
  81.     }
  82.  
  83.     /* when this does its return the CatchSignal above will be automatically popped */
  84.     if (code = CatchSignal())
  85.         return code;
  86.     
  87.     Failer();
  88. }
  89.  
  90.  
  91. main()
  92. {
  93.     short                code;
  94.     volatile long        registerLong = 0;
  95.     
  96.     InitUFailure();
  97.  
  98.     if (code = CatchSignal()) {
  99.         fprintf(stdout, "Signal caught in main(); code = %d, registerLong = %d\n",
  100.             code, registerLong);
  101.         return 0;
  102.     }
  103.     
  104.     registerLong = 0xffff;
  105.     
  106.     Signal(Value());
  107. }
  108.